<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #action="{row}">
      <el-button @click="to_edit(row)" size="small">详情</el-button>
    </template>
    <template #value_limit="{row}">
      <div v-for="(values,key) in row.value_limit" :key="key">
        <span>{{key}}</span><span> 属于 </span>
        <el-tag v-for="(v,i) in values" :key="i">{{v}}</el-tag>
      </div>
    </template>
  </list_url>
</template>
<script>
import s94Mjs from "s94-js";
import {iframe, in_right_admin, toast} from "@/js/tool.js";
import {_config, decode_config, json_decode_object} from "@/js/s94_tool.js";
import {ajax} from "@/js/api_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    if(in_right_admin('EnumTitle/add')) btns['添加'] = function(){ iframe('enum_title/edit') }
    if(in_right_admin('EnumTitle/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }

    let model_name_options = decode_config(_config('model_map'), {content_key:'label', value_key:'name'})
    let forms_config = {
      model_name: {label:'模型',name:'where[model_name][eq]',type:'select_one',value:'', config:model_name_options},
      group: {label:'枚举组',name:'where[group][like]',type:'text_one',value:''},
    };
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', order:true},
      model_name: {name:'model_name', label:'模型名', width:'100px'},
      group: {name:'group', label:'枚举组', width:'100px'},
      title: {name:'title', label:'标题', width:'100px'},
      value_limit: {name:'value_limit', label:'值限制', min_width:'100px', type:'value_limit'},
      sort:{name:'sort', label:'排序', type:'text_one', width:'60px', can_edit:in_right_admin('EnumTitle/edit'), onchange:this.edit},
    };

    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'创建时间', width:'165px'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'80px', type:'action', fixed:'right'};

    return {
      config: {
        api: '/admin/enum_title/'+(is_del?'recycle':'list'),
        title: "枚举列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          list.forEach(row=>{
            row.value_limit = json_decode_object(row.value_limit_json);
            Object.keys(row.value_limit).forEach(k=>{
              if (typeof row.value_limit[k] === 'string'){
                row.value_limit[k] = row.value_limit[k].split(',');
              }
            })
          })
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
    }
  },
  methods: {
    to_edit(row){
      let can_edit = in_right_admin('EnumTitle/edit') && ~[0,row.tenant_id].indexOf(_config('this_tenant.id'))
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      this.iframe('edit?'+s94Mjs.param_encode(param))
    },
    edit(row,col,v){
      let app = this;
      let can_edit = in_right_admin('EnumTitle/edit') && ~[0,row.tenant_id].indexOf(_config('this_tenant.id'))
      if(!can_edit) {
        return toast.fail('无权操作');
      }
      ajax(`/admin/enum_title/edit?id=${row.id}&${col.name}=${v}`, function (res){
        if (res.data==0) app.$refs['list_url'].list();
      })
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

  }
}
</script>
